<template>
	<view>
		<!-- pages/pay/pay.wxml -->
		<!-- 收货地址 -->
		<view>
			<view class="user_info_row" @click="handleChoose">
				<view class="user_info" v-for="item in addInfo" :key="index">
					<view>收 货 人： {{item.name}}</view>
					<view>联系方式：{{item.phone}}</view>
					<view>收货地址：{{item.address}}</view>
				</view>
			</view>
		</view>
		<!-- 购物车内容 -->
		<view class="cart_content">
			<view class="cart_title">确认订单</view>
			<view class="cart_main">
				<view v-for="(item, index) in cart" :key="index" class="cart_item">
					<!-- 商品图片 -->
					<navigator class="cart_img_wrap"
						:url="'/pages/goods_detail/goods_detail?goods_id=' + item.goods_id">
						<image :src="item.goods_small_logo" mode="widthFix"></image>
					</navigator>
					<!-- 商品信息 -->
					<view class="cart_info_wrap">
						<navigator class="goods_name"
							:url="'/pages/goods_detail/goods_detail?goods_id=' + item.goods_id">
							{{item.goods_name}}
						</navigator>
						<view class="goods_price_wrap">
							<view class="goods_price">￥{{item.goods_price}}</view>
							<view class="cart_num_tool">
								<view class="goods_num">X {{item.num}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部工具栏 -->
		<view class="footer_tool">
			<!-- 总价格 -->
			<view class="total_price_wrap">
				<view class="total_price">
					合计：
					<text class="total_price_text">￥{{totalPrice}}</text>
				</view>
				<view>包含运费</view>
			</view>
			<!-- 支付 -->
			<view class="order_pay_wrap" @tap="handleOrderPay">支付（{{totalNum}}）</view>
		</view>
	</view>
</template>

<script>
	// pages/pay/pay.js
	import {
		getSetting,
		openSetting,
		chooseAddress,
		showModal,
		showToast,
		requestPayment
	} from "../../utils/asyncWx.js";
	import regeneratorRuntime from "../../lib/runtime/runtime";
	import {
		request
	} from "../../request/index.js";

	export default {
		data() {
			return {
				addInfo: [{
					name: "赵嘻嘻",
					phone: "13313535620",
					address: "天津市西青区宾水西道天津市大学软件学院"
				}],
				address: {},
				cart: [],
				totalPrice: 0,
				totalNum: 0
			};
		},

		components: {},
		props: {},

		onShow() {
			// 1. 获取缓存中的收货信息
			const address = wx.getStorageSync("address");

			// 获取缓存中的购物车数据
			let cart = wx.getStorageSync("cart") || [];

			// 过滤后的购物车数组
			cart = cart.filter(v => v.checked);
			this.setData({
				address
			});

			// 总价格 总数量
			let totalPrice = 0;
			let totalNum = 0;
			cart.forEach(v => {
				totalPrice += v.num * v.goods_price;
				totalNum += v.num;
			});
			this.setData({
				cart,
				totalPrice,
				totalNum,
				address
			});
		},

		methods: {
			// 支付功能
			async handleOrderPay() {
				// try {
				// 	// 1. 判断缓存中有没有token(令牌 唯一身份的意思)
				const token = wx.getStorageSync("token"); // 2. 判断

				if (token) {
					// 2. 支付成功了 跳转到订单页面
					wx.navigateTo({
						url: '/pages/order/order'
					});
					// 3. 创建订单
					const order_price = this.totalPrice;
					const consignee_addr = this.address.all;
					const cart = this.cart;
					let goods = [];
					cart.forEach(v => goods.push({
						goods_id: v.goods_id,
						goods_number: v.num,
						goods_price: v.goods_price
					}));
					const orderParams = {
						order_price,
						consignee_addr,
						goods
					};

					// 4. 准备发送请求 创建订单 获取订单编号	
					const order_number = await request({
						url: "/my/orders/create",
						method: "POST",
						data: orderParams
					});

					// 手动删除缓存中 已经支付了的商品
					let newCart = wx.getStorageSync("cart");
					newCart = newCart.filter(v => !v.checked);
					wx.setStorageSync("cart", newCart);
					// return;
				}
			}
		}
	};
</script>
<style>
	@import "./pay.css";
</style>
